/************************************************************************
                        NurbsTesselator.cpp - Copyright Claudio Botta

This library is free software; you can redistribute it and/or modify it 
under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2.1 of the License, or 
(at your option) any later version.
This library is distributed in the hope that it will be useful, but 
WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 
or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public 
License for more details.

You should have received a copy of the GNU Lesser General Public License 
along with this library; if not, write to the 
Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, 
Boston, MA 02110, USA

This file was generated on Sun Apr 27 2008 at 15:29:34
The original location of this file is geomagick/NurbsTesselator.cpp
**************************************************************************/

#include "NurbsTesselator.h"

Triangles* NurbsTesselator::process(Nurbs* inNurbs)
{
	Triangles* output = new Triangles();
	//float tolerance = 0.1;
	BasicList< PLib::Point_nD<float,3> > Vertices;
	BasicList<int> Edges;
	BasicList<PLib::Point_nD<float,3> > normals;
	typedef PLib::Point_nD<float,3> vt;
	
	PLib::NurbsSubSurface<float>*  NurbsSubSurf = new PLib::NurbsSubSurface<float>(*(inNurbs->getNurbs()));
	PLib::BasicArray<vt> tmpVertexArray;
	NurbsSubSurf->drawSubdivisionPoints(tmpVertexArray,0.001f);
	//NurbsSubSurf->drawSubdivisionPS("test.ps",0.01f);
	// PLib::BasicArray<vt>::const_iterator VertexIterator= tmpVertexArray.begin();
	#if DEBUG
	cout << "The size of the Array is: " << tmpVertexArray.size() << endl; 
	#endif

	 //(*(inNurbs->getNurbs()))(0.1,0.1);
	
	 PLib::HPoint_nD<float,3> curPoint;
	 PLib::Point_nD<float,3>  curNormal;
	 int a = 25;
	 int b = 25;
	 for(int i = 0; i < a+1; i++)
         {

		for(int j = 0; j < b+1; j++)
		{
			
			curPoint = (*(inNurbs->getNurbs()))((float)i/(float)a,(float)j/(float)b);
			Vector3d newVertex  = *(new Vector3d);
			newVertex.x = curPoint.x()/curPoint.w();
			newVertex.y = curPoint.y()/curPoint.w();
			newVertex.z = curPoint.z()/curPoint.w();
			
			output->addVertexCoordinates(newVertex);

			curNormal = (*(inNurbs->getNurbs())).normal((float)i/(float)a,(float)j/(float)b);
			Vector3d newNormal  = *(new Vector3d);
			newNormal.x = curNormal.x();
			newNormal.y = curNormal.y();
			newNormal.z = curNormal.z();
			newNormal.normalize();
			output->addNormal(newNormal);
			//cout << "Normal: " << newNormal.x <<"," << newNormal.y << "," << newNormal.z <<";" << endl; 
			

		}
	 }	

	
	for(int i = 0; i < a; i++)
	{
		for(int j = 0; j < b; j++)
		{
		 Triangle newTriangle = *(new Triangle());
			newTriangle.Vertices.push_back(i*(b+1)+j);
			newTriangle.Vertices.push_back((i+1)*(b+1)+j+1);
			newTriangle.Vertices.push_back(i*(b+1)+j+1 );
			
	
			newTriangle.Normals.push_back(i*(b+1)+j);
			newTriangle.Normals.push_back((i+1)*(b+1)+j+1);
			newTriangle.Normals.push_back(i*(b+1)+j+1 );
			
			
			output->addTriangle(newTriangle);
			
			
			newTriangle = *(new Triangle());
			
			newTriangle.Vertices.push_back(j+(b+1)*i);
			
			
			
			newTriangle.Vertices.push_back(j+(b+1)*(i+1));
			newTriangle.Vertices.push_back(j+1+(b+1)*(i+1));	
			
			
			newTriangle.Normals.push_back(j+(b+1)*i);
					
			newTriangle.Normals.push_back(j+(b+1)*(i+1));
			newTriangle.Normals.push_back(j+1+(b+1)*(i+1));	


			output->addTriangle(newTriangle);
						

			
		
		}
	}
	/*
	while(VertexIterator < tmpVertexArray.end())
	{
		//Triangle newTriangle = *(new Triangle());
		Vector3d newVertex  = *(new Vector3d);
		newVertex.x = VertexIterator->x();
		newVertex.y = VertexIterator->y();
		newVertex.z = VertexIterator->z();

		output->addVertexCoordinates(newVertex);
		VertexIterator++;
	} */

	return output;
}
	
	/*
	vector<Vector3d> VertexCoordinates = *(inPolygons->getVertexCoordinates());
	vector<Vector3d> Normals 	  =  *(inPolygons->getNormals());
	vector<Vector2d> TextureCoordinates = *(inPolygons->getTextureCoordinates());
	vector<Polygon> PolygonList  = *(inPolygons->getPolygonList());

			Triangle newTriangle = *(new Triangle());
			newTriangle.Vertices.push_back(v1);
			newTriangle.Vertices.push_back(v2);
			newTriangle.Vertices.push_back(v3);
			
			newTriangle.Normals.push_back(n1);
			newTriangle.Normals.push_back(n2);
			newTriangle.Normals.push_back(n3);

			newTriangle.TextureCoordinates.push_back(t1);
			newTriangle.TextureCoordinates.push_back(t2);
			newTriangle.TextureCoordinates.push_back(t3);
			output->addTriangle(newTriangle);
		
		
	
	*/

	// Copy all the other attributes
	/*
	foreach(Vector3d VertexCoordinate, VertexCoordinates)
	{
		output->addVertexCoordinates(VertexCoordinate);
	}	
	
	foreach(Vector3d Normal, Normals)
	{
		output->addNormal(Normal);
	}
	
	foreach(Vector2d TextureCoordinate, TextureCoordinates)
	{
		output->addTextureCoordinates(TextureCoordinate);
	}
	*/
	//return output;
//}
